package com.example.isometric_tower_defense;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Enemy {
    public int x, y;
    private int speed = 1; // 降低速度
    private int health = 100;
    private int maxHealth = 100;
    private int pathIndex = 0;
    private List<Point> path;
    
    public Enemy(List<Point> path) {
        this.path = path;
        if (path != null && !path.isEmpty()) {
            this.x = path.get(0).x;
            this.y = path.get(0).y;
        }
    }
    
    public void move() {
        // 如果没有路径或已到达终点，不移动
        if (path == null || path.isEmpty() || pathIndex >= path.size() - 1) {
            return;
        }
        
        // 获取当前目标点
        Point target = path.get(pathIndex + 1);
        
        // 计算到目标点的距离
        int dx = target.x - x;
        int dy = target.y - y;
        
        // 如果距离小于速度，则直接移动到目标点
        if (Math.abs(dx) < speed && Math.abs(dy) < speed) {
            x = target.x;
            y = target.y;
            pathIndex++; // 移动到下一个路径点
        } else {
            // 否则按速度朝目标点移动
            if (dx != 0) {
                x += dx > 0 ? Math.min(speed, dx) : Math.max(-speed, dx);
            }
            if (dy != 0) {
                y += dy > 0 ? Math.min(speed, dy) : Math.max(-speed, dy);
            }
        }
    }
    
    public void update() {
        // 更新敌人状态
        move();
    }
    
    public void render(Graphics2D g, int isoX, int isoY) {
        // 绘制敌人为红色圆形
        g.setColor(Color.RED);
        g.fillOval(isoX - 10, isoY - 10, 20, 20);
        
        // 绘制边框
        g.setColor(Color.BLACK);
        g.drawOval(isoX - 10, isoY - 10, 20, 20);
        
        // 绘制生命值条
        if (health < maxHealth) {
            g.setColor(Color.RED);
            int barWidth = 20;
            int barHeight = 3;
            int barX = isoX - barWidth/2;
            int barY = isoY - 20;
            g.fillRect(barX, barY, (int)(barWidth * (health / (double)maxHealth)), barHeight);
            
            g.setColor(Color.WHITE);
            g.drawRect(barX, barY, barWidth, barHeight);
        }
    }
    
    // Getters and Setters
    public int getHealth() {
        return health;
    }
    
    public void setHealth(int health) {
        this.health = health;
    }
    
    public boolean isFinished() {
        return pathIndex >= path.size() - 1;
    }
    
    public void setSpeed(int speed) {
        this.speed = speed;
    }
}